package be.vdabantwerpen.gow.servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

import be.vdabantwerpen.gow.entities.GOWUser;
import be.vdabantwerpen.gow.exceptions.UserNotFoundException;
import be.vdabantwerpen.gow.services.UserServiceImpl;
//import be.vdabantwerpen.gow.services.UserService;

/**
 * Servlet implementation class RegistrationServlet
 */
public class RegistrationServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final String VIEW = "/WEB-INF/JSP/posts/poststream.jsp";
	private static final String REDIRECT_VIEW = "/guestbook";
	private final be.vdabantwerpen.gow.services.UserService gowUserService = 
                new UserServiceImpl();
	
	UserService userService;
	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		GOWUser gowUser = null;
		HttpSession session = request.getSession();
		
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
				
		if (user != null) {
			String userId = user.getUserId();
			request.setAttribute("user", user);
                        try {
                            gowUser = gowUserService.findByName(userId);
                        }
                        catch(UserNotFoundException unfe){
                            gowUser = new GOWUser();
                            gowUser.setUserName(userId);
                            gowUser.setFirstName("");
                            gowUser.setLastName("");
                            gowUser.setNumber(0);
                            gowUser.setStreet("");
                            gowUser.setTown("");
                            gowUser.setUserId(0);
                        }
                        session.setAttribute("user", gowUser);
			request.getRequestDispatcher(VIEW).forward(request, response);
		} else {
			response.sendRedirect(userService.createLoginURL(request.getRequestURI()));
		}		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<String> incorrect = new ArrayList<String>();
		int number = 0, postalCode = 0;
		String firstName = null, lastName = null, street = null, town = null, userId = null, password = null, repeatPassword = null;
		
		// VERIFICATIONS
		if(request.getParameter("firstName") == null || request.getParameter("firstName").isEmpty()) {
			incorrect.add("Gelieve een voornaam in te vullen");
		} else {
			firstName = request.getParameter("firstName");
		}
		
		if(request.getParameter("lastName") == null || request.getParameter("lastName").isEmpty()) {
			incorrect.add("Gelieve een familienaam in te vullen");
		} else {
			lastName = request.getParameter("lastName");
		}
		
		if(request.getParameter("street") == null || request.getParameter("street").isEmpty()) {
			incorrect.add("Gelieve een straat in te vullen");
		} else {
			street = request.getParameter("street");
		}
		
		if(request.getParameter("number") == null || request.getParameter("number").isEmpty()) {
			incorrect.add("Gelieve een huisnummer in te vullen");
		} else {
			number = Integer.parseInt(request.getParameter("number"));
		}
		
		if(request.getParameter("postalCode") == null || request.getParameter("postalCode").isEmpty()) {
			incorrect.add("Gelieve een postcode in te vullen");
		} else {
			postalCode = Integer.parseInt(request.getParameter("postalCode"));
		}
		
		if(request.getParameter("town") == null || request.getParameter("town").isEmpty()) {
			incorrect.add("Gelieve een gemeente in te vullen");
		} else {
			town = request.getParameter("town");
		}
		
		if(request.getParameter("userId") == null || request.getParameter("userId").isEmpty()) {
			incorrect.add("Gelieve een gebruikersnaam in te vullen");
		} else {
			userId = request.getParameter("userId");
			List<User> users = new ArrayList<User>();
			//users = userService.findAll();
			for(User username : users) {
//				if(userId.equalsIgnoreCase(username.getUserId())) {
//					incorrect.add("Gebruikersnaam is al in gebruik");
//				}
			}
		}
		
		if(request.getParameter("password") == null || request.getParameter("password").isEmpty()) {
			incorrect.add("Gelieve een wachtwoord in te vullen");
		} else {
			password = request.getParameter("password");
		}
		
		if(request.getParameter("repeatPassword") == null || request.getParameter("repeatPassword").isEmpty()) {
			incorrect.add("Gelieve het wachtwoord ter controle nog eens in te geven");
		} else {
			repeatPassword = request.getParameter("repeatPassword");
			if(!password.equals(repeatPassword)) {
				incorrect.add("De wachtwoorden komen niet overeen");
			}
		}
		
		// SETTLEMENT
		if(incorrect.isEmpty()) {
			//User user = new User(firstName, lastName, street, number, postalCode, town, userId, password);
			//userService.create(user);
			
			HttpSession session = request.getSession();
			//session.setAttribute("user", user);
			response.sendRedirect(request.getContextPath() + REDIRECT_VIEW);
		} else {
			request.setAttribute("incorrect", incorrect);
			request.getRequestDispatcher(VIEW).forward(request, response);
		}
	}

}
